/**
 * 对称裁切图片到特定尺寸以下的特定比例(对半逐级压缩避免产生锯齿)
 */
function resizeImgToBelowSize(file, belowSize, toWHRatio, callback) {
    if (typeof FileReader == 'undefined') {
        alert("当前浏览器不支持FileReader接口");
        return;
    }
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function (e) {
        var image = new Image();
        image.src = e.target.result;
        image.onload = function () {
            var canvas = document.createElement('canvas');
            var context = canvas.getContext('2d');
            var canExecute = false;
            var newWidth = image.width;
            var newHeight = image.height;
            while (newWidth > belowSize || newHeight > belowSize) {
                //对半压缩才不会产生锯齿
                canExecute = true;
                newWidth = newWidth * 0.5;
                newHeight = newHeight * 0.5;
            }
            if(newWidth / newHeight != toWHRatio){
                canExecute = true;
            }
            if (canExecute) {
                if (newWidth / newHeight > toWHRatio) {
                    canvas.width = newHeight * toWHRatio;
                    canvas.height = newHeight;
                    var x = -Math.round((newWidth - canvas.width) * 0.5);
                    var y = 0;
                    context.clearRect(x, y, newWidth, newHeight);
                    context.drawImage(image, x, y, newWidth, newHeight);
                } else {
                    canvas.width = newWidth;
                    canvas.height = newWidth / toWHRatio;
                    var x = 0;
                    var y = -Math.round((newHeight - canvas.height) * 0.5);
                    context.clearRect(x, y, newWidth, newHeight);
                    context.drawImage(image, x, y, newWidth, newHeight);
                }
                canvas.toBlob(function (blob) {
                    callback(blob);
                }, file.type || 'image/jpeg');
            } else {
                callback(file);
            }
        }
    }
}

/**
 * 等比压缩图片到特定尺寸以下，保留完整图片不裁切(对半逐级压缩避免产生锯齿)
 */
function compressImgToBelowSize(file, belowSize, callback) {
    if (typeof FileReader == 'undefined') {
        alert("当前浏览器不支持FileReader接口");
        return;
    }
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function (e) {
        var image = new Image();
        image.src = e.target.result;
        image.onload = function () {
            var canExecute = false;
            var newWidth = image.width;
            var newHeight = image.height;
            while (newWidth > belowSize || newHeight > belowSize) {
                //对半压缩才不会产生锯齿
                canExecute = true;
                newWidth = newWidth * 0.5;
                newHeight = newWidth * 0.5;
            }
            if (canExecute) {
                var canvas = document.createElement('canvas');
                var context = canvas.getContext('2d');
                canvas.width = newWidth;
                canvas.height = newHeight;
                context.clearRect(0, 0, newWidth, newHeight);
                context.drawImage(image, 0, 0, newWidth, newHeight);
                canvas.toBlob(function (blob) {
                    callback(blob);
                }, file.type || 'image/jpeg');
            } else {
                callback(file);
            }
        }
    }
}

/**
 * 最优方案裁切图片到指定尺寸(会产生明显的锯齿，建议使用对半缩小到1000像素以下的的方案：resizeImgToBelowSize())
 */
function resizeImg(file, toWidth, toHeight, callback) {
    if (typeof FileReader == 'undefined') {
        alert("当前浏览器不支持FileReader接口");
        return;
    }
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function (e) {
        var image = new Image();
        image.src = e.target.result;
        image.onload = function () {
            var canExecute = false;
            var canvas = document.createElement('canvas');
            var context = canvas.getContext('2d');
            var newWidth = 0;
            var newHeight = 0;
            if (image.width / image.height > toWidth / toHeight) {
                if (image.height > toHeight) {
                    canExecute = true;
                    newHeight = toHeight;
                    newWidth = Math.round(toHeight * image.width / image.height);
                    canvas.width = toWidth;
                    canvas.height = toHeight;
                    var x = -Math.round((newWidth - canvas.width) * 0.5);
                    var y = 0;
                    context.clearRect(x, y, newWidth, newHeight);
                    context.drawImage(image, x, y, newWidth, newHeight);
                }
            } else {
                if (image.width > toWidth) {
                    canExecute = true;
                    newWidth = toWidth;
                    newHeight = Math.round(toWidth * image.height / image.width);
                    canvas.width = toWidth;
                    canvas.height = toHeight;
                    var x = 0;
                    var y = -Math.round((newHeight - canvas.height) * 0.5);
                    context.clearRect(x, y, newWidth, newHeight);
                    context.drawImage(image, x, y, newWidth, newHeight);
                }
            }
            if (canExecute) {
                canvas.toBlob(function (blob) {
                    callback(blob);
                }, file.type || 'image/jpeg');
            } else {
                callback(file);
            }
        }
    }
}

/**
 * 限制最大尺寸等比压缩图片，保留完整图片不裁切(会产生明显的锯齿，建议使用对半缩小的方案compressImgToBelow1000())
 */
function compressImg(file, maxWidth, maxHeight, callback) {
    if (typeof FileReader == 'undefined') {
        alert("当前浏览器不支持FileReader接口");
        return;
    }
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function (e) {
        var image = new Image();
        image.src = e.target.result;
        image.onload = function () {
            var canExecute = false;
            var newWidth = 0;
            var newHeight = 0;
            if (image.width / image.height > maxWidth / maxHeight) {
                if (image.height > maxHeight) {
                    canExecute = true;
                    newHeight = maxHeight;
                    newWidth = Math.round(maxHeight * image.width / image.height);
                }
            } else {
                if (image.width > maxWidth) {
                    canExecute = true;
                    newWidth = maxWidth;
                    newHeight = Math.round(maxWidth * image.height / image.width);
                }
            }
            if (canExecute) {
                var canvas = document.createElement('canvas');
                var context = canvas.getContext('2d');
                canvas.width = newWidth;
                canvas.height = newHeight;
                context.clearRect(0, 0, newWidth, newHeight);
                context.drawImage(image, 0, 0, newWidth, newHeight);
                canvas.toBlob(function (blob) {
                    callback(blob);
                }, file.type || 'image/jpeg');
            } else {
                callback(file);
            }
        }
    }
}

/**
 * file转base64
 * @param file(files.file[0])
 * @param callback(base64)
 */
function fileToBase64(file, callback) {
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function (e) {
        callback(this.result.substring(this.result.indexOf(',') + 1));
    }
}